//
//  SEGEditUserViewModel.m
//  SEGViewModel
//
//  Created by 山刘 on 2020/11/24.
//

#import "SEGEditUserViewModel.h"

@interface SEGEditUserViewModel()
@property (nonatomic, strong, readwrite) RACCommand *requestCommand;
@property (strong, nonatomic, readwrite) RACSubject *messageSubject;

@property (nonatomic, strong, readwrite) RACCommand *authCommand;
@property (nonatomic, strong, readwrite) RACCommand *userUpdateCommand;
@property (nonatomic, strong, readwrite) RACCommand *delUserCommand;
@end

@implementation SEGEditUserViewModel
#pragma mark - Init
-(instancetype)initWithType:(SEGEnumEditUsersType)type{
    if (self = [super init]) {
        _type = type;
        [self racInit];
    }
    return self;
}

-(void)dealloc{
    [self.requestCommand rac_deallocDisposable];
    [self.messageSubject rac_deallocDisposable];
    
    [self.authCommand rac_deallocDisposable];
    [self.userUpdateCommand rac_deallocDisposable];
    [self.delUserCommand rac_deallocDisposable];
}

- (void)racInit {
    
    @weakify(self);
    _requestCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            SEGQueryauthinfoRoleauthmapModel *roleAuthMap = [SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo;
            [[SEGSelectauthorityroleManager sharedManager] requestSelectauthorityroleWithCommunityid:kAccountCommunityID
                                                                                        enterpriseId:roleAuthMap.enterpriseId
                                                                                             organId:roleAuthMap.organId
                                                                                              propId:roleAuthMap.propId
                                                                                            finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendNext:@{@"code":@"-1",@"msg":@""}];
                    [subscriber sendCompleted];
                    return;
                }
                
                [subscriber sendNext:@{@"code":@"0",@"msg":@""}];
                [subscriber sendCompleted];
                return;
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _authCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(id  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            
            if (self.name.length <= 0) {
                [SEGAlertView showTmepMessageWithMessage:@"请输入账号名称"];
                [subscriber sendCompleted];
                return nil;
            }
            
            if (self.name.length > 10) {
                [SEGAlertView showTmepMessageWithMessage:@"姓名格式不正确"];
                [subscriber sendCompleted];
                return nil;
            }
            
            if (self.tel.length <= 0) {
                [SEGAlertView showTmepMessageWithMessage:@"请输入手机号码"];
                [subscriber sendCompleted];
                return nil;
            }
            
            if (self.tel.length != 11) {
                [SEGAlertView showTmepMessageWithMessage:@"手机号格式不正确"];
                [subscriber sendCompleted];
                return nil;
            }
            
            
            if (!self.roleData) {
                [SEGAlertView showTmepMessageWithMessage:@"请选择角色"];
                [subscriber sendCompleted];
                return nil;
            }
            
            
            SEGQueryauthinfoRoleauthmapModel *roleAuthMap = [SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo;
            
            if (self.type == SEGEnumEditUsersType_Add) {
                
                [self isShowProgressHUD:YES];
                
                NSString *userType = @"";
                
                userType = [self.roleData.isAdmin isEqualToString:@"1"] ? @"1":@"2";
                
                [[SEGUserinsertuserManager sharedManager] requestUserinsertuserWithCustname:self.name
                                                                                    userTel:self.tel
                                                                               enterpriseId:roleAuthMap.enterpriseId
                                                                                     roleId: self.roleData.roleId
                                                                                communityId:kAccountCommunityID
                                                                                     propId:[SEGQueryauthinfoManager sharedManager].QueryAuthInfo.data.userInfo.propId
                                                                                   userType:userType
                                                                                   finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                    [self isShowProgressHUD:NO];
                    
                    [self showMessage:errorMessage];
                    
                    if (!isSuccessed) {
                        [subscriber sendError:nil];
                        [subscriber sendCompleted];
                        return;
                    }
                    
                    [subscriber sendNext:@{@"code":@"1",@"msg":@""}];
                    [subscriber sendCompleted];
                }];
            }
            
            if (self.type == SEGEnumEditUsersType_Edit) {
                
                if ([self.roleData.isAdmin isEqualToString:@"1"]) {
                    [subscriber sendNext:@{@"code":@"2",@"msg":@""}];
                    [subscriber sendCompleted];
                    return nil;
                }
                
                //其他类型 直接掉接口 不需要弹框
                [self.userUpdateCommand execute:self.name];
                [subscriber sendError:nil];
                [subscriber sendCompleted];
            }
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _userUpdateCommand  = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...x
            [self isShowProgressHUD:YES];
            //[SVProgressHUD show];
            [[SEGUserinsertuserManager sharedManager] requestUpdateUserCommonWithUserCustRelIds:self.enterpriseUserId
                                                                                         roleId: self.roleData.roleId
                                                                                       userName:input
                                                                                       finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                [self isShowProgressHUD:NO];
                
                if (!isSuccessed) {
                    [self showMessage:errorMessage];
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                    return;
                }
                
                if ([self.roleData.isAdmin isEqualToString:@"1"]) {
                    [subscriber sendNext:@{@"code":@"1",@"msg":@""}];
                    [subscriber sendCompleted];
                    return;
                }
                
                [self showMessage:errorMessage];
                [subscriber sendNext:@{@"code":@"2",@"msg":@""}];
                [subscriber sendCompleted];
                return;
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
    
    _delUserCommand  = [[RACCommand alloc] initWithSignalBlock:^RACSignal * _Nonnull(NSString*  _Nullable input) {
        return [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
            @strongify(self);
            
            // Code here...
            [[SEGUserinsertuserManager sharedManager] requestDeleteUserWithUserCustRelIds:self.enterpriseUserId
                                                                                 finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                
                if(errorMessage && errorMessage.length > 0) [self showMessage:errorMessage];;
                
                if (!isSuccessed) {
                    [subscriber sendError:nil];
                    [subscriber sendCompleted];
                    return;
                }
                
                [subscriber sendNext:nil];
                [subscriber sendCompleted];
                return;
            }];
            
            return [RACDisposable disposableWithBlock:^{
            }];
        }];
    }];
}

#pragma mark - Publish Methods

#pragma mark - Private Methods
-(void)isShowProgressHUD:(BOOL)show {
    [self.messageSubject sendNext:@{@"code": show ? @"1" : @"2"}];
}
-(void)showMessage:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"3",@"msg":msg}];
}
-(void)customProgress:(NSString*)msg {
    [self.messageSubject sendNext:@{@"code":@"4",@"msg": (msg && msg.length > 0 ) ? msg : @"" }];
}

#pragma mark - Getter
-(RACSubject *)messageSubject{
    if (!_messageSubject) {
        _messageSubject = [RACSubject subject];
    }
    return _messageSubject;
}
#pragma mark - Stter
@end
